package com.learning.spring.cloud.alibaba.ribbon.order.web.controller;

import com.learning.spring.cloud.alibaba.ribbon.order.web.model.Order;
import com.learning.spring.cloud.alibaba.ribbon.order.web.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: UserController
 * Description:
 * Date: 2021/7/25 23:03 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * @param order
     * @return
     */
    @PostMapping("/save")
    public Integer insert(@RequestBody Order order) {
        return orderService.insert(order);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Integer deleteByPrimaryKey(@PathVariable("id") Long id) {
        return orderService.deleteByPrimaryKey(id);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public Order selectByPrimaryKey(@PathVariable("id") Long id) {
        return orderService.selectByPrimaryKey(id);
    }

    @GetMapping("/query/user/{userId}")
    public List<String> queryByUserId(@PathVariable("userId") Long userId) {
        return orderService.queryByUserId(userId).stream().map(Order::getName).collect(Collectors.toList());
    }
}
